"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const index = require("../panel/index.cjs");
const antDesignVue = require("ant-design-vue");
require("./tree-list.vue.cjs");
const index$1 = require("../empty/index.cjs");
const lodashEs = require("lodash-es");
const tree = require("../../utils/tree.cjs");
const treeList_vue_vue_type_script_setup_true_lang = require("./tree-list.vue2.cjs");
const _hoisted_1 = { class: "me-tree-search" };
const _hoisted_2 = { class: "me-tree-search-input" };
const _hoisted_3 = { class: "me-tree-search-content" };
const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "me-tree-search"
  },
  __name: "tree-search",
  props: {
    treeProps: { default() {
      return {};
    } },
    placeholder: { default: "请输入关键字" },
    treeEvents: { default() {
      return {};
    } },
    showList: { type: Boolean }
  },
  emits: ["select"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const treeRef = vue.ref();
    const searchValue = vue.ref("");
    const expandedKeyList = vue.ref([]);
    const checkedKeyList = vue.ref([]);
    const selectedKeyList = vue.ref([]);
    const treeList = vue.ref([]);
    let cloneTreeList = [];
    const arrayList = vue.ref([]);
    const isSearching = vue.ref(false);
    const onChange = lodashEs.debounce(() => {
      if (!searchValue.value.trim()) {
        resetTree();
      } else {
        queryTree(searchValue.value);
      }
    }, 200);
    vue.watch(() => props.treeProps.treeData, () => {
      const { treeProps: { treeData } } = props;
      treeList.value = lodashEs.cloneDeep(treeData);
      cloneTreeList = lodashEs.cloneDeep(treeData);
    }, { immediate: true });
    vue.watch(() => [props.treeProps.expandedKeys, props.treeProps.checkedKeys, props.treeProps.selectedKeys], () => {
      expandedKeyList.value = props.treeProps.expandedKeys;
      checkedKeyList.value = props.treeProps.checkedKeys;
      selectedKeyList.value = props.treeProps.selectedKeys;
    }, { immediate: true });
    const queryTree = (keyword) => {
      isSearching.value = true;
      searchValue.value = keyword;
      const { showList } = props;
      if (!showList) {
        treeList.value = tree.fuzzyQueryTree(cloneTreeList, keyword);
        if (treeList.value.length > 0) {
          addExpandKeys(treeList.value);
        }
      } else {
        arrayList.value = tree.getFuzzyPathByKey(keyword, cloneTreeList, { keyProp: "title" });
      }
    };
    const addExpandKeys = (list) => {
      expandedKeyList.value = [];
      const go = (list2) => {
        list2 == null ? void 0 : list2.forEach((item) => {
          if (item.children && item.children.length > 0) {
            expandedKeyList.value.push(item.key);
            go(item.children);
          }
        });
      };
      go(list);
    };
    const resetTree = () => {
      treeList.value = lodashEs.cloneDeep(cloneTreeList);
      expandedKeyList.value = [];
      isSearching.value = false;
      searchValue.value = "";
    };
    const onClickNode = (node) => {
      emits("select", vue.toRaw(node));
    };
    __expose({
      treeInstance: treeRef.value,
      queryTree,
      resetTree
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createBlock(vue.unref(index.Panel), null, {
        default: vue.withCtx(() => [
          vue.createVNode(vue.unref(index.PanelContent), null, {
            default: vue.withCtx(() => [
              vue.createElementVNode("div", _hoisted_1, [
                vue.createElementVNode("div", _hoisted_2, [
                  vue.renderSlot(_ctx.$slots, "search", {}, () => [
                    vue.createVNode(vue.unref(antDesignVue.InputSearch), {
                      onChange: vue.unref(onChange),
                      value: searchValue.value,
                      "onUpdate:value": _cache[0] || (_cache[0] = ($event) => searchValue.value = $event),
                      placeholder: _ctx.placeholder
                    }, null, 8, ["onChange", "value", "placeholder"])
                  ])
                ]),
                vue.createElementVNode("div", _hoisted_3, [
                  _ctx.showList ? (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 0 }, [
                    treeList.value.length > 0 && !isSearching.value ? (vue.openBlock(), vue.createBlock(vue.unref(antDesignVue.Tree), vue.mergeProps({
                      key: 0,
                      ref_key: "treeRef",
                      ref: treeRef
                    }, _ctx.treeProps, vue.toHandlers(_ctx.treeEvents), {
                      "tree-data": treeList.value,
                      "expanded-keys": expandedKeyList.value,
                      "onUpdate:expandedKeys": _cache[1] || (_cache[1] = ($event) => expandedKeyList.value = $event),
                      "checked-keys": checkedKeyList.value,
                      "onUpdate:checkedKeys": _cache[2] || (_cache[2] = ($event) => checkedKeyList.value = $event),
                      "selected-keys": selectedKeyList.value,
                      "onUpdate:selectedKeys": _cache[3] || (_cache[3] = ($event) => selectedKeyList.value = $event)
                    }), vue.createSlots({ _: 2 }, [
                      _ctx.$slots.title ? {
                        name: "title",
                        fn: vue.withCtx((attrs) => [
                          vue.renderSlot(_ctx.$slots, "title", vue.normalizeProps(vue.guardReactiveProps(attrs)))
                        ]),
                        key: "0"
                      } : void 0,
                      _ctx.$slots.switcherIcon ? {
                        name: "switcherIcon",
                        fn: vue.withCtx((attrs) => [
                          vue.renderSlot(_ctx.$slots, "switcherIcon", vue.normalizeProps(vue.guardReactiveProps(attrs)))
                        ]),
                        key: "1"
                      } : void 0
                    ]), 1040, ["tree-data", "expanded-keys", "checked-keys", "selected-keys"])) : arrayList.value.length > 0 && isSearching.value ? (vue.openBlock(), vue.createBlock(treeList_vue_vue_type_script_setup_true_lang.default, {
                      key: 1,
                      onClick: onClickNode,
                      keyword: searchValue.value,
                      list: arrayList.value
                    }, null, 8, ["keyword", "list"])) : (vue.openBlock(), vue.createBlock(vue.unref(index$1.Empty), {
                      key: 2,
                      description: "暂无数据"
                    }))
                  ], 64)) : (vue.openBlock(), vue.createElementBlock(vue.Fragment, { key: 1 }, [
                    treeList.value.length > 0 ? (vue.openBlock(), vue.createBlock(vue.unref(antDesignVue.Tree), vue.mergeProps({
                      key: 0,
                      ref_key: "treeRef",
                      ref: treeRef
                    }, _ctx.treeProps, vue.toHandlers(_ctx.treeEvents), {
                      "tree-data": treeList.value,
                      "expanded-keys": expandedKeyList.value,
                      "onUpdate:expandedKeys": _cache[4] || (_cache[4] = ($event) => expandedKeyList.value = $event),
                      "checked-keys": checkedKeyList.value,
                      "onUpdate:checkedKeys": _cache[5] || (_cache[5] = ($event) => checkedKeyList.value = $event),
                      "selected-keys": selectedKeyList.value,
                      "onUpdate:selectedKeys": _cache[6] || (_cache[6] = ($event) => selectedKeyList.value = $event)
                    }), vue.createSlots({ _: 2 }, [
                      _ctx.$slots.title ? {
                        name: "title",
                        fn: vue.withCtx((attrs) => [
                          vue.renderSlot(_ctx.$slots, "title", vue.normalizeProps(vue.guardReactiveProps(attrs)))
                        ]),
                        key: "0"
                      } : void 0,
                      _ctx.$slots.switcherIcon ? {
                        name: "switcherIcon",
                        fn: vue.withCtx((attrs) => [
                          vue.renderSlot(_ctx.$slots, "switcherIcon", vue.normalizeProps(vue.guardReactiveProps(attrs)))
                        ]),
                        key: "1"
                      } : void 0
                    ]), 1040, ["tree-data", "expanded-keys", "checked-keys", "selected-keys"])) : (vue.openBlock(), vue.createBlock(vue.unref(index$1.Empty), {
                      key: 1,
                      description: "暂无数据"
                    }))
                  ], 64))
                ])
              ])
            ]),
            _: 3
          })
        ]),
        _: 3
      });
    };
  }
});
exports.default = _sfc_main;
//# sourceMappingURL=tree-search.vue2.cjs.map
